from PIL import Image
import os
sheet_path = r'..\asset\sheet\tilemap.png'
out_path = r'..\asset\city'
img_size = (16, 16)
sheet_margin = 1


if __name__ == '__main__':
    if not os.path.exists(out_path):
        os.makedirs(out_path)
    with open(sheet_path, 'rb') as f:
        origin_img = Image.open(f)
        origin_size = origin_img.size
        for col in range(int((origin_size[0] + sheet_margin) / (img_size[0] + sheet_margin))):
            _x = col * (img_size[0] + sheet_margin)
            for row in range(int((origin_size[1] + sheet_margin) / (img_size[1] + sheet_margin))):
                # if  (row in range(0, 2 + 1) and col in range(16, 22 + 1)):
                    _y = row * (img_size[1] + sheet_margin)
                    new_img = origin_img.crop((_x, _y, _x + img_size[0], _y + img_size[1]))
                    row_str = str(row)
                    col_str = str(col)
                    while len(row_str) < 2:
                        row_str = f"0{row_str}"
                    while len(col_str) < 2:
                        col_str = f"0{col_str}"

                    new_img.save(f'{out_path}\\{row_str}_{col_str}.png', 'png')
